$(function() {
	mui.init({
		pullRefresh: {
			container: refreshContainer, //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
			up: {
				height: 50, //可选.默认50.触发上拉加载拖动距离
				auto: false, //可选,默认false.自动上拉加载一次
				contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
				contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
				callback: puful //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
			}
			//,下拉刷新
			// down : {
			//     height:50,//可选,默认50px.下拉刷新控件的高度,
			//     auto: false,//可选,默认false.首次加载自动上拉刷新一次
			//     callback :pufulDown
			//  }
		}
	});
	var record = 0; //每次返回的数据的长度

	//请求json
	var req = {
		'userid': localStorage.getItem('userid'),
		'page': '1', //页码
		'pagenum': Dpage, //每页条数
		'token': localStorage.getItem('token')
	}
	//上拉加载刷新
	function puful() {
		req.page = parseInt(req.page) + 1; //页码
		Request();
		console.log(record);

		if (record < req.pagenum) {
			this.endPullupToRefresh(true);
		} else {
			setTimeout(function() {
				mui('#refreshContainer').pullRefresh().enablePullupToRefresh();
				mui('#refreshContainer').pullRefresh().endPullupToRefresh();
			}, 1500)
		}
	}
	// function pufulDown() {
	//     setTimeout(function(){
	//         window.location.reload();
	//     },500);
	// }

	//内容区加载
	mui.ready(function() {
		//加载框
		mui.showLoading("正在加载..", ".warn-list");
		Request();
		mui('.warn-list').on('tap', 'li a', function() {
			var dtcode = $(this).find('h3 b').html();

			// 先移除,后保存
			localStorage.removeItem('dtcode');
			localStorage.setItem('dtcode', dtcode);

			mui.openWindow({
				url: 'html/Yearly/Yearly-lift-detail.html',
				id: 'Yearly-lift-detail.html'
			});
		})
	});

	// 重新获取数据或触发刷新
	window.addEventListener("pageRefresh", function() {
		Request();
	});

	//ajax请求处理数据
	function Request() {
		var url = domain + "/dtwbgl/wbyearcheck.h5";
		$.ajax(url, {
			data: req,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型,
			timeout: 30000, //超时时间
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			},
			async: true, //true异步加载
			success: function(data) {
				if (data.code == 2001) {
					console.log(data);
					//mui.alert(JSON.stringify(data));
					var ref = data.data.rows;
					record = ref.length;

					var len = ref.length;
					var html = '';
					for (var i = 0; i < len; i++) {
						//数据处理 
						var theDays = ref[i].days; //是否超期
						var theClass = '';
						var theText = '';
						// var productionDate=ref[i].days;
						//var theYear=regString(ref[i].dtzhuccode);
						var theYear = ref[i].age;
						if (theDays < 0) {
							theClass = 'active';
							theDays *= (-1);
							theText = '已超期';
						} else {
							theClass = '';
							theText = '距离超期';
						}
						html +=
							`
                <li>
                    <a>                   
                        <div class="warn-bianma">
                            <h2>物业名称：${ref[i].wyname}</h2>   
                            <div class="warn-bianma-state">
                                <p class="${theClass}">
                                    <i>${theText}</i><span>${theDays}天</span>
                                </p>
                            </div>
                        </div>
												 <div class="warn-zhucema">
														<h3>内部编码：${ref[i].fsydwnbbm}</h3> 
												</div>
                        <div class="warn-zhucema">
                            <h3>注册代码：<b>${ref[i].dtzhuccode}</b></h3>
                        </div>
                        <div class="warn-hx">
                            <i></i><i></i>
                        </div>
                        <div class="warn-time mui-row">
                            <div class="mui-col-xs-6">
                              <p class="">${ref[i].nextyearcheck}日截止</p>
                            </div>
                            <div class="mui-col-xs-6">
                                
                            </div>
                        </div>
                    </a>
                </li>
                       `;
					}

					//隐藏加载框
					mui.hideLoading(function() { //隐藏后的回调函数  
					});
					$('.warn-list').append(html);
					//console.log(record);
				} else if (data.code == 2002) {
					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../login.html'
						})
					});
					//console.log(data); 
				} else {
					mui.alert(data.msg, function() {
						mui.openWindow({
							url: '../../404.html'
						})
					});
				}

			},
			error: function(xhr, type, errorThrown) { //异常处理； console.log(type);
				mui.openWindow({
					url: '../../404.html',
					id: '404.html'
				})
			}

		});
	}

})
